package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.ApplyTcDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.ApplyTcInfo;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class DealApplyTC extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String token = webUtil.getCookie(request, "LoginCookie");
		Connection conn = DBUtil.getConnection();
		
		int aid = TextUtil.String2int(request.getParameter("id"),0);
		int reslut = TextUtil.String2int(request.getParameter("result"), -1);
		String rcontent = request.getParameter("resultcontent");
		
		if (conn==null) {
			Repost.dberror(request, response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		ApplyTcDao adao = dao.getApplyTcDao();
		ApplyTcInfo info =adao.getapplyinfo2(aid);
		
		UserInfo uinfo = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		if (uinfo==null) {
			Repost.nologin(response);
			return;
		} else if (uinfo.getUlevel()<Constant.ULEVEL_admin) {
			Repost.message("你没权限管理！", request, response);
			return;
		}
		
		
		if (info==null) {
			Repost.message("核审已被通过或不存在！", request, response);
			return;
		}
		
		UserInfo uinfo2 = udao.getInfo(info.getUserid());
		if (uinfo2==null) {
			Repost.message("该用户已不存在！", request, response);
			return;
		}
		info.setResult(reslut);
		info.setResultcontent(rcontent);
		if (adao.assessor(uinfo2,info)){
			Repost.success("servlet_listapplytc?onepagecount=10&page=1", request, response);
		} else {
			Repost.message("核审失败", request, response);
		}
		
	}

}
